      1  import java.util.Collection;

      2  import java.util.ArrayList;

      3  import java.util.TreeSet;

      4

      5  /**

      6     This program demonstrates classes from the Java collections framework.

      7  */

      8  public class CollectionsDemo

      9  {

     10     public static void main(String[] args)

     11     {

     12        System.out.println("Working with an ArrayList");

     13        workWith(new ArrayList<String>());

     14        System.out.println("Working with a TreeSet");

     15        workWith(new TreeSet<String>());

     16     }

     17

     18     /**

     19        Shows how to work with a collection of strings.

     20        @param coll a collection from the Java collections framework

     21     */

     22     public static void workWith(Collection<String> coll)

     23     {

     24        coll.add("Harry");

     25        coll.add("Sally");

     26        coll.add("Fred");

     27        coll.add("Wilma");

     28        coll.add("Harry");

     29        System.out.println(coll);

     30        System.out.print("Removing Harry and Tom: ");

     31        System.out.print(coll.remove("Harry") + " ");

     32        System.out.println(coll.remove("Tom"));

     33        System.out.print("Looking for Harry and Sally: ");

     34        System.out.print(coll.contains("Harry") + " ");

     35        System.out.println(coll.contains("Sally"));

     36        for (String s : coll)

     37        {

     38           System.out.println(s);

     39        }

     40     }

     41  }